<?php


namespace App\Http\Controllers\Admin\WeekStar;

use App\Exceptions\BasicException;
use \App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class WeekStarGiftController extends BaseController
{
    /**
     * 获取周星所有礼物
     * @return string
     */
    public function weekGift()
    {
        $gifts  =  app('repo_gift')->weekGiftList();
        return Response::sendData($gifts);
    }

    /**
     * @api {get} /week/star/gift/list 获取周星榜礼物配置
     * @apiVersion 0.1.1
     * @apiName 获取周星榜礼物
     * @apiGroup 周星榜管理
     *
     * @apiParam {number} id
     * @apiParam {number} start_at     202011
     * @apiParam {number} end_at     202039
     * @apiParam {number} page     礼物ID
     * @apiParam {number} perPage     礼物ID
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 98040  拉取礼物失败
     * @apiErrorExample 错误响应
     *
     *       "code": 200,
     * "data": {
     * "current_page": 1,
     * "data": [
     * {
     * "id": 16,
     * "gift_id_1": 90,
     * "gift_id_2": 88,
     * "create_time": 1596509603,
     * "update_time": 1596509603,
     * "weeks": 202032,
     * "period": 1,
     * "status": 2,
     * "deleted": 0,
     * "room_types": [
     * {
     * "id": 4,
     * "room_type_name": "聊天"
     * },
     * {
     * "id": 20,
     * "room_type_name": "娱乐"
     * }
     * ],
     * "start_time": "2020-08-03 00:00:00",
     * "end_time": "2020-08-10 00:00:00",
     * "gift_data1": {
     * "id": 90,
     * "name": "城堡"
     * },
     * "gift_data2": {
     * "id": 88,
     * "name": "I LOVE YOU"
     * }
     * }
     *
     */

    public function getWeekStarGiftListApi(Request $request)
    {
        $startAt = $request->input('start_at', 0);
        $endAt = $request->input('end_at', 0);
        if ($startAt > 0) {
            $sYear = date('Y', $startAt);
            $sWeek = date('W', $startAt);
            $eYear = date('Y', $endAt);
            $eWeek = date('W', $endAt);
            $startAt = $sYear * 100 + $sWeek;
            $endAt = $eYear * 100 + $eWeek;
        }
        $id = $request->input('id', 0);
        $page = $request->input('page', 1);
        $gift = $request->input('gift','');
        $status = $request->input('status',0);
        $perPage = $request->input('perPage', 20);
        $giftList = app('repo_week_star')->getWeekStarGiftList($startAt, $endAt, $id,$gift,$status,$page, $perPage);
        foreach ($giftList as $gift) {
            $weeks = $gift['weeks'];
            $week = $weeks % 100;
            $year = ($weeks - $week) / 100;
            $timeZone = $this->weekday($year, $week);
            $gift['start_time'] = date('Y-m-d H:i:s', $timeZone['start']);
            $gift['end_time'] = date('Y-m-d H:i:s', $timeZone['end']);

            $room_types = [];

            if ($gift['room_types']) {
                $room_type_array = array_filter(explode(',', $gift['room_types']));
                if ($room_type_array) {
                    $room_type_result = app('model_room_type')->whereIn('id', $room_type_array)->select([
                        'id',
                        'room_type_name',
                    ])->get();
                    if (!$room_type_result->isEmpty()) {
                        $room_types = $room_type_result;
                    }
                }
            }
            $gift['room_types'] = $room_types;
            $gift['period_text'] = $year.'年第'.$week.'周';
        }

        return Response::sendData([
            'gift_list' =>  $giftList,
            'last'      =>  app('repo_week_star')->getLastWeek()
        ]);
    }
    /**
     * @api {put} /week/star/gift/update 更新周星榜礼物配置
     * @apiVersion 0.1.1
     * @apiName 更新周星榜礼物
     * @apiGroup 周星榜管理
     *
     * @apiParam {number} id     道具ID
     * @apiParam {number} weeks     202039
     * @apiParam {number} gift_id_1     礼物ID
     * @apiParam {number} gift_id_2     礼物ID
     * @apiParam {number} period     多少期
     * @apiParam {string} room_types     参与本期周星榜的房间类型，多个用,隔开
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 98041  添加礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 98041,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function upWeekStarGiftApi(Request $request)
    {
        $id = $request->input('id', 0);
        $weeks = $request->input('weeks', 0);
        $weeks = date('oW', $weeks);
        $giftId1 = $request->input('gift_id_1', 0);
        $giftId2 = $request->input('gift_id_2', 0);
        $period = $request->input('period', 0);
        $room_types = $request->input('room_types', '');
        if ($id == 0) {
            app('repo_week_star')->addWeekStarGift($giftId1, $giftId2, $weeks, $period, $room_types);

            return Response::sendData(true);
        }
        app('repo_week_star')->upWeekStarGift($id, $giftId1, $giftId2, $weeks, $period, $room_types);

        app('redis')->del('weekstar:gift:'.$weeks);

        return Response::sendData(true);
    }

    /**
     * @api {put} /week/star/gift/del 删除周星榜礼物配置
     * @apiVersion 0.1.1
     * @apiName 删除周星榜礼物
     * @apiGroup 周星榜管理
     *
     * @apiParam {number} ids     [1,2,3,4,5]
     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 98041  删除礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 98041,
     *          "message": "errorMsg"
     *      }
     *
     */

    public function delWeekStarGiftApi(Request $request)
    {
        $ids = $request->input('ids', []);
        if (empty($ids)) {
            throw new BasicException(0, '请选择要删除的数据');
        }
        app('repo_week_star')->delWeekStarGift($ids);

        return Response::sendData(true);
    }

    public function weekday($year, $week = 1)
    {
        $first_day_of_year = mktime(0, 0, 0, 1, 1, $year);
        $first_week_day = date('N', $first_day_of_year);
        $first_week_number = date('W', $first_day_of_year);
        $format = 'Y/m/d';
        if ($first_week_number == 1) {
            $day = (1 - ($first_week_day - 1)) + 7 * ($week - 1);
            $start_date = date($format, mktime(0, 0, 0, 1, $day, $year));
            $end_date = date($format, mktime(0, 0, 0, 1, $day + 6, $year));
        } else {
            $day = (9 - $first_week_day) + 7 * ($week - 1);
            $start_date = date($format, mktime(0, 0, 0, 1, $day, $year));
            $end_date = date($format, mktime(0, 0, 0, 1, $day + 6, $year));
        }

        return ['start' => strtotime($start_date), 'end' => strtotime($end_date) + 86399];
    }
}
